package com.powernode.business.controller;

import com.powernode.business.domain.Rent;
import com.powernode.business.service.RentService;
import com.powernode.common.core.controller.BaseController;
import com.powernode.common.core.domain.AjaxResult;
import com.powernode.common.core.domain.model.LoginUser;
import com.powernode.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("business/rent")
@Api(tags = "出租单管理")
public class RentController extends BaseController {

    @Autowired
    private RentService rentService;

    @GetMapping("list")
    @ApiOperation("查询出租表")
    public TableDataInfo list(Rent rent) {
        //开启分页
        startPage();
        //分门店处理，每个门店的管理员只能看到自己门店的管理员
        //超级管理员可以查看所有的出租单
        LoginUser loginUser = getLoginUser();
        List<Rent> rentList = rentService.selectRent(rent, loginUser);
        return getDataTable(rentList);
    }


    @GetMapping("{id}")
    @ApiOperation("出租单修改之前的数据回显")
    public AjaxResult getRentById(@PathVariable("id") Integer id){
        Rent rent = rentService.selectByPrimaryKey(id);
        return AjaxResult.success(rent);
    }


    @PutMapping
    @ApiOperation("出租单修改")
    public AjaxResult updateRent(@RequestBody Rent rent){
        //拿到当前用户
        LoginUser loginUser = getLoginUser();
        rent.setUpdateBy(loginUser.getUsername());
        rent.setUpdateTime(new Date());
        return toAjax(rentService.updateByPrimaryKey(rent));
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除出租单")
    public AjaxResult deleteRent(@PathVariable("id") List<Integer> id){
        //拿到当前用户
        LoginUser loginUser = getLoginUser();
        return toAjax(rentService.deleteRent(id,loginUser));
    }

}
